// ProjectName: go-learn-demo
// PackageName: main
// Description:
// Author: hy.bai
// Date: 2025-08-16 星期六 16:07

package main

import (
	"fmt"
	"log/slog"
	"net/http"
	"strings"
)

func handleHome(resp http.ResponseWriter, req *http.Request) {
	// req.ParseForm()解析参数，默认是不会解析
	// 获取的参数是：http://127.0.0.1:8080/?page=1&pagesize=10
	if err := req.ParseForm(); err != nil {
		slog.Error(fmt.Sprintf("解析参数发生错误:%s", err.Error()))
		return
	}
	for key, val := range req.Form {
		fmt.Println("key:", key)
		fmt.Println("val:", strings.Join(val, ""))
	}
	// 把数据写入resp结果中，响应给前端
	fmt.Fprintf(resp, "hello world")
}
func main() {
	// 注册请求
	http.HandleFunc("/", handleHome)
	addr := ":8080"
	slog.Info("服务访问地址: http://127.0.0.1" + addr)
	if err := http.ListenAndServe(addr, nil); err != nil {
		slog.Error(fmt.Sprintf("启动服务失败,原因是:%s", err.Error()))
	}
}
